package com.chencs.biz.shop.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.category.service.CategoryService;
import com.chencs.biz.shop.service.ShopService;
import com.chencs.vo.biz.product.FavoritesModel;
import com.chencs.vo.biz.shop.NewShopModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by fangzehua on 2017/8/15.
 */
@RequestMapping("/shop")
@RestController
public class ShopContorller {

    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "/doShopList", method = RequestMethod.GET)
    public ModelAndView doShopList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/shop/shop_list");
        return modelAndView;
    }





    @RequestMapping(value = "/shopList", method = RequestMethod.GET)
    public String shopList(){
        String retStr = "";
        try {
            retStr = shopService.shopList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/doNewShopList", method = RequestMethod.GET)
    public ModelAndView doNewShopList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/shop/new_shop_list");
        return modelAndView;
    }


    @RequestMapping(value = "/newShopList", method = RequestMethod.GET)
    public String newShopList(){
        String retStr = "";
        try {
            retStr = shopService.newshopList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/doEditor/{code}", method = RequestMethod.GET)
    public ModelAndView doRolePage(@PathVariable("code") String code , Model model) {
        NewShopModel mode = null;
        try {
            mode = JSON.parseObject(shopService.findNewShopModel(code + ""), NewShopModel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/shop/new_shop_editor");
        model.addAttribute("mode", mode);
        return modelAndView;

    }

    /**
     * 跳轉添加分类页面
     * @return
     */
    @RequestMapping(value = "/doAddShopPage", method = RequestMethod.GET)
    public ModelAndView doAddShopPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/biz/shop/new_shop_add");
        return modelAndView;
    }


//
//        @RequestMapping(value = "/newShop/{sid}", method = RequestMethod.GET)
//    public String getSpecialProductById(@PathVariable("sid") String sid){
//        String retStr = "";
//        try {
//            retStr = shopService.findNewShopModel(sid);
//        } catch (Exception e) {
//            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
//            e.printStackTrace();
//        }
//        return retStr;
//    }


    @RequestMapping(value = "/addShopModel", method = RequestMethod.POST)
    public String addShopModel(@RequestBody NewShopModel model){
        String retStr = "";
        try {
            retStr = shopService.addShopModel(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/updateShopModel", method = RequestMethod.POST)
    public String updateShopModel(@RequestBody  NewShopModel model){
        String retStr = "";
        try {
            retStr = shopService.updateShopModel(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/delShopModel", method = RequestMethod.POST)
    public String delShopModel(@RequestBody String sid){
        String retStr = "";
        try {
            retStr = shopService.delShopModel(sid);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

}

